package com.cy.pj.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.dao.SysUserDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.entity.SysUser;
import com.cy.pj.sys.service.SysUserService;
import com.cy.pj.sys.vo.SysUserDeptVo;

@Service
public class SysUserServiceImpl extends BasePageServiceImpl<SysUserDeptVo> implements SysUserService {

	private SysUserDao sysUserDao;
	private SysUserRoleDao sysUserRoleDao;

	@Autowired
	public SysUserServiceImpl(SysUserDao sysUserDao,SysUserRoleDao sysUserRoleDao) {
		super(sysUserDao);
		this.sysUserDao=sysUserDao;
		this.sysUserRoleDao=sysUserRoleDao;
	}

	@Override
	public int validById(Integer id, Integer valid, String modifiedUser) {
		if(id==null||id<=0) {
			throw new ServiceException("参数不合法,id="+id);
		}
		if(valid!=1&&valid!=0) {
			throw new ServiceException("参数不合法,valie="+valid);
		}
		if(StringUtils.isEmpty(modifiedUser)) {
			throw new ServiceException("修改用户不能为空");
		}
		int rows=0;
		try{
			rows=sysUserDao.validById(id, valid, modifiedUser);
		}catch(Throwable e){
			e.printStackTrace();
			throw new ServiceException("底层正在维护");
		}
		if(rows==0) {
			throw new ServiceException("此记录可能已经不存在");
		}
		return rows;
	}

	@Override
	@Transactional
	public int saveObject(SysUser entity, Integer[] roleIds) {
		if(entity==null) {
			throw new ServiceException("保存对象不能为空");
		}
		if(StringUtils.isEmpty(entity.getUsername())) {
			throw new ServiceException("用户名不能为空");
		}
		if(StringUtils.isEmpty(entity.getPassword())) {
			throw new ServiceException("密码不能为空");
		}
		if(roleIds==null || roleIds.length==0) {
			throw new ServiceException("至少要为用户分配一个角色");
		}

		String salt = UUID.randomUUID().toString();
		entity.setSalt(salt);
		SimpleHash sh=new SimpleHash("MD5", entity.getPassword(), salt, 1);
		entity.setPassword(sh.toHex());

		int rows = sysUserDao.insertObject(entity);
		int userRoleRows=sysUserRoleDao.insertObjects(entity.getId(), roleIds);	
//		if (userRoleRows>0) {
//			throw new ServiceException("添加用户信息失败");
//		}
		return rows;
	}

	@Override
	public Map<String, Object> findObjectById(Integer userId) {
		if(userId==null||userId<=0) {
			throw new ServiceException("参数数据不合法,userId="+userId);
		}
		SysUserDeptVo user = sysUserDao.findObjectById(userId);
		List<Integer> roleIds = sysUserRoleDao.findRoleIdsByUserId(userId);
		Map<String, Object> map=new HashMap<>();
		map.put("user", user);
		map.put("roleIds", roleIds);
		return map;
	}
	
	@Override
	public int updateObject(SysUser entity, Integer[] roleIds) {
		if(entity==null) {
			throw new IllegalArgumentException("保存对象不能为空");
		}
		if(StringUtils.isEmpty(entity.getUsername())) {
			throw new IllegalArgumentException("用户名不能为空");
		}
		if(roleIds==null||roleIds.length==0) {
			throw new IllegalArgumentException("必须为其指定至少一个角色");
		}
		//...
		int rows = sysUserDao.updateObject(entity);
		sysUserRoleDao.deleteObjectsByUserId(entity.getId());
		sysUserRoleDao.insertObjects(entity.getId(), roleIds);
		return rows;
			
	}
	
	@Override
	public int updatePassword(String password, String newPassword, String cfgPassword) {
		if (StringUtils.isEmpty(newPassword)) {
			throw new IllegalArgumentException("新密码不能为空");
		}
		if (StringUtils.isEmpty(cfgPassword)) {
			throw new IllegalArgumentException("确认密码不能为空");
		}
		if (!newPassword.equals(cfgPassword)) {
			throw new IllegalArgumentException("两次输入的密码不相等");
		}
		if (StringUtils.isEmpty(password)) {
			throw new IllegalArgumentException("原密码不能为空");
		}
		SysUser user=(SysUser)SecurityUtils.getSubject().getPrincipal();
		SimpleHash sh = new SimpleHash("MD5", password, user.getSalt(), 1);
		
		if (!user.getPassword().equals(sh.toHex())) {
			throw new IllegalArgumentException("原密码不正确");
		}
		String salt = UUID.randomUUID().toString();
		sh=new SimpleHash("MD5", newPassword, salt, 1);
		int rows = sysUserDao.updatePassword(sh.toHex(), salt, user.getId());
		if (rows==0) {
			throw new ServiceException("修改失败");
		}
		return rows;
	}
}
